package cn.tedu.sims.service.impl;

import cn.tedu.sims.ex.ServiceException;
import cn.tedu.sims.mapper.*;
import cn.tedu.sims.pojo.dto.InternalUnitDTO;
import cn.tedu.sims.pojo.dto.UnitPowerUpdateDTO;
import cn.tedu.sims.pojo.entity.InternalUnit;
import cn.tedu.sims.pojo.entity.InternalUnitPower;
import cn.tedu.sims.pojo.vo.InternalUnitListItemVO;
import cn.tedu.sims.pojo.vo.InternalUnitStandardVO;
import cn.tedu.sims.pojo.vo.LocationListItemVO;
import cn.tedu.sims.pojo.vo.UserListItemVO;
import cn.tedu.sims.service.InternalUnitService;
import cn.tedu.sims.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class InternalUnitServiceImpl implements InternalUnitService {
    @Autowired
    private InternalUnitMapper internalUnitMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UnitPowerMapper unitPowerMapper;
    @Autowired
    private LocationMapper locationMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<InternalUnitListItemVO> list() {
        List<InternalUnitListItemVO> list= internalUnitMapper.list();
        return list;
    }

    @Override
    public InternalUnitStandardVO selectById(Long id) {
        InternalUnitStandardVO vo=internalUnitMapper.selectById(id);
        return vo;
    }

    @Override
    public void deleteById(Long id) {
        int count=userMapper.countByUnit(id);
        if (count>0) {
            throw new ServiceException(ServiceCode.ERR_CONFLICT,"删除失败,用户关联");
        }

        int sum=internalUnitMapper.deleteById(id);
        if (sum!=1){
            throw new ServiceException(ServiceCode.ERR_DELETE,"删除失败,服务器繁忙");
        }

        int sum1=unitPowerMapper.deleteByUnitPower(id);
        if (sum1<=0){
            throw new ServiceException(ServiceCode.ERR_DELETE,"删除失败,服务器繁忙");
        }
    }

    @Override
    public void insert(InternalUnitDTO internalUnitDTO) {
       int count= internalUnitMapper.countByName(internalUnitDTO.getName());
       if (count>0){
           throw new ServiceException(ServiceCode.ERR_CONFLICT,"添加失败,单位名字重复");
       }

        InternalUnit iu=new InternalUnit();
        BeanUtils.copyProperties(internalUnitDTO,iu);
        iu.setGmtCreate(LocalDateTime.now());
        int sum=internalUnitMapper.insert(iu);
        if (sum!=1){
            throw new ServiceException(ServiceCode.ERR_INSERT,"添加失败,服务器繁忙");
        }

        InternalUnitPower iup=new InternalUnitPower();
        iup.setInternalUnitId(iu.getId());

        int sum1= unitPowerMapper.insertByUnitId(iup);
        if (sum1!=1){
            throw new ServiceException(ServiceCode.ERR_INSERT,"添加失败,服务器繁忙");
        }
    }

    @Override
    public List<InternalUnitListItemVO> selectByName(String name) {
        return internalUnitMapper.selectByName(name);
    }

    @Override
    public List<UserListItemVO> selectByUser(Long id) {
        return internalUnitMapper.selectByUser(id);
    }

    @Override
    public List<LocationListItemVO> selectByRegion() {
        return locationMapper.list();
    }

    @Override
    public List<Long> selectPowerExist(Long id) {
        return internalUnitMapper.selectPowerExist(id);
    }

    @Override
    public void updatePower(UnitPowerUpdateDTO u) {
        if (u.getPowers()==null){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"修改失败,权限不能为空");
        }

        int countById = internalUnitMapper.countById(u.getInternalUnitId());
        if (countById!=1){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"修改失败,单位不存在");
        }

        int countByPower = roleMapper.countByPower(u.getPowers());
        if (countByPower!=u.getPowers().size()){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"修改失败,权限不存在");
        }


        int deleteByRolePower = internalUnitMapper.deleteByRolePower(u.getInternalUnitId());


        int sum=0;
        for (Long powerId: u.getPowers()){
            internalUnitMapper.insertRolePower(powerId,u.getInternalUnitId());
            sum+=1;

        }
        if (sum!=u.getPowers().size()){
            throw new ServiceException(ServiceCode.ERR_INSERT,"修改失败,服务器繁忙");
        }
    }
}
